package com.atguigu.functioncodeagain

object SimplyFunction {
  def main(args: Array[String]): Unit = {


      //def pr(x:String):Unit=print(x)
      def pr(x: String=>Unit):Unit=x("wzx")
      pr((x:String)=>println(x))

    def f(func: String=>Unit): Unit=func("nishishabi")
    f((a:String)=>println(a))
    f(a=>println(a))
    f(println(_))
    f(println)
    //定义一个二元运算函数,只操作1和2两个数,具体运算通过参数传入
    def cal(func: (Int,Int)=>Int): Int={func(1,2)}

    println(cal((x: Int, y: Int) => x + y))
    println(cal((x, y) => x + y))
    println(cal(_+_))

    //值传递
    val a=(x:Int,y:Int)=>x+y
    val b = cal _
    //作为参数传递
    def cal1(x:Int,y:Int,func: (Int,Int)=>Int): Int={func(x,y)}
    def func(x:Int,y:Int): Int={x+y}
    cal1(3,5,(x,y)=>x+y)
    cal1(3,5,_+_)
    //作为函数返回
    def func1()={
      (x:Int,y:Int)=>{x+y}
    }
    //函数的嵌套与闭包
    def f1(x:Int): Int=>(Int,Int)=>Int ={ s=>{(c,d)=>{x+s+c+d }}}
    //柯里化
    def f2(x:Int)(y:Int)(z:Int,d:Int): Int={x+y+z+d}

    println(f1(1)(2)(3, 4))
    println(f2(1)(2)(3, 4))
    //普通while循环
    var dd = 10
    while(dd>=1){
      println(dd)
      dd=dd-1
    }
    //闭包实现while功能的函数
    def myWhile(condition: =>Boolean):(=>Unit)=>Unit ={
      def doLoop(op: =>Unit): Unit={
        if(condition){
          op
          myWhile(condition)(op)
        }
      }
      doLoop
    }
    dd = 10
    myWhile(dd>=1)({println(dd)
      dd=dd-1})
    //匿名函数
    def myWhile1(condition: =>Boolean):(=>Unit)=>Unit ={op=>{ if(condition){ op
          myWhile1(condition)(op)} } }
    dd = 10
    myWhile1(dd>=1)({println(dd)
      dd=dd-1})
    //柯里化
    def myWhile2(condition: =>Boolean)(op: =>Unit):Unit={
      if(condition){
        op
        myWhile2(condition)(op)
      }
    }
    //闭包的应用
    def add(a:Int,b:Int):Int=a+b
    def add1(a:Int):Int=a+4
    def add2(a:Int): Int=>Int ={
      def addf(x:Int):Int={
        a+x
      }
      addf
    }
    def add3(a: Int)(x: Int): Int=a+x
    val arr = Array("spark","hello","java","hadoop")
    def getLength(arr:Array[String],func: String=>Int): Array[Int]={
      for(a<- arr)yield func(a)
    }
    getLength(arr,_.length)
    val arr1 = Array(1,4,7,9,10,6,8)
    def ouShu(arr:Array[Int],func: Int=>Boolean):Array[Int] ={
      for(a<-arr if(func(a)))yield{
        a
      }
    }

    println(ouShu(arr1, _ % 2 == 0).toList)
    val  arr3 =Array("zs 30 3500","lisi 25 1800","zhaoliu 29 4500")
    def big(arr: Array[String],func: String=>Int): String={
      var tmpString = arr(0)
      var tmp = func(arr(0))
      for(a<-arr){
        if(tmp<func(a)) {
          tmp=func(a)
          tmpString=a
        }
      }
      tmpString
    }

    println(big(arr3, _.split(" ")(2).toInt))
    val arr5 = Array(10,4,6,10,2)
    def cal14(arr:Array[Int],func: (Int,Int)=>Int): Int={
      var tmp = arr(0)
      for(i<- 1 until arr.length){
        tmp = func(tmp,arr(i))
      }
      tmp
    }

    println(cal14(arr5, _ + _))

    //普通while循环
    var rr = 10
    while(rr>=1){
      println(rr)
      rr=rr-1
    }
   // Map("d"->f).getOrElse()

    def myW(condition: =>Boolean):(=>Unit)=>Unit ={
op=>{
        if(condition){
          op
          myW(condition)(op)
        }
      }

    }
    def myW1(condition: =>Boolean)(op: =>Unit): Unit = {
      if(condition){
        op
        myW1(condition)(op)
      }
    }











































  }

}
